Repository navigation
fix(serialization): normalize content-type parameters, whitespace, and casing in JSON factories and registries - #2259
Conversation
…ivity in JsonParseNodeFactory Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…ivity in JsonSerializationWriterFactory Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…ty delimiters in ParseNodeFactoryRegistry Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…undant cleanup in SerializationWriterFactoryRegistry Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…y tests for JsonParseNodeFactory Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…y tests for JsonSerializationWriterFactory Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…-case tests for ParseNodeFactoryRegistry Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
…vity tests for SerializationWriterFactoryRegistry Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
|
Following up on the content-type normalization updates for multipart (#2229), text (#2236), and form (#2239),
This PR brings the JSON factories to parity, fixes both registries while preserving compatibility with custom registered keys and |
| for (final Map.Entry<String, ParseNodeFactory> entry : | ||
| contentTypeAssociatedFactories.entrySet()) { | ||
| final String key = entry.getKey(); | ||
| final ParseNodeFactory factory = entry.getValue(); | ||
| if (key != null && factory != null && key.equalsIgnoreCase(contentType)) { | ||
| return entry; | ||
| } | ||
| } | ||
| return null; |
There was a problem hiding this comment.
no, this leads to o(n) lookup on every missing entry instead of o(1). We should normalize upon adding instead
There was a problem hiding this comment.
Good catch. In commit 14966428, we introduced CaseInsensitiveMap<V> extends HashMap<String, V> backing contentTypeAssociatedFactories in both ParseNodeFactoryRegistry and SerializationWriterFactoryRegistry.
Keys are now normalized upon adding (put / putAll strips parameters, trims whitespace, and converts to lowercase). Lookups in getParseNode() and getSerializationWriter() now perform direct O(1) hash map operations (get(vendorSpecificContentType) and get(cleanedContentType)), eliminating the linear scan over entrySet() on cache misses.
…or O(1) lookups Signed-off-by: Hardik Kaurani <hardikkaurani1@gmail.com>
|
I have updated the PR in
The full Gradle build ( |
Summary
Completes RFC 2045 / RFC 9110 content-type normalization across
JsonParseNodeFactory,JsonSerializationWriterFactory,ParseNodeFactoryRegistry, andSerializationWriterFactoryRegistry, bringing JSON factories to parity with the form (#2239), text (#2236), and multipart (#2229) factories and fixing crash/lookup bugs in both factory registries.Root Cause
JsonParseNodeFactoryandJsonSerializationWriterFactory:contentTypewith strict equality (!contentType.equals(validContentType)), rejecting valid content types carrying MIME parameters (e.g.,application/json; charset=utf-8), optional whitespace around;, or case variations (e.g.,APPLICATION/JSON).JsonSerializationWriterFactory(Gson)also had a copy-paste message ("gson contentType cannot be null") inObjects.requireNonNull.ParseNodeFactoryRegistry:contentType.split(";")[0]threw an uncaughtArrayIndexOutOfBoundsExceptionfor delimiter-only inputs such as";"or";;"becauseString.split(";")drops trailing empty strings."APPLICATION/JSON; charset=utf-8","application/json ; charset=utf-8", or"Application/Vnd.Example+JSON ; charset=utf-8"failed withClientException.SerializationWriterFactoryRegistry:getVendorSpecificContentTypedid not trim whitespace around;or handle delimiter-only inputs cleanly.getCleanedVendorSpecificContentType(cleanedContentType)was invoked twice redundantly on the fallback path.Changes
JsonParseNodeFactory&JsonSerializationWriterFactory:contentType.split(";", 2)[0].trim()) and match case-insensitively (equalsIgnoreCase(validContentType)).JsonSerializationWriterFactory(Gson)null-check message withJsonParseNodeFactory(Gson)("parameter gson cannot be null").ParseNodeFactoryRegistry&SerializationWriterFactoryRegistry:2and.trim(), throwingClientExceptioncleanly on empty/delimiter-only media types instead ofArrayIndexOutOfBoundsException.Locale.ROOTlowercase fast path, and case-insensitive entry fallback, forwarding the matched registered key to the underlying factory (preserving compatibility with custom factories andBackingStoreSerializationWriterProxyFactory).getCleanedVendorSpecificContentTypecall inSerializationWriterFactoryRegistry.JsonParseNodeFactoryTestsandJsonSerializationWriterFactoryTests.FactoryRegistryExceptionTestwith parameterized case-insensitivity, whitespace, vendor subtype, mixed-case registration, backing store bypass, and delimiter-only regression tests for both registries.Verification
./gradlew --no-daemon build: BUILD SUCCESSFUL across all components (abstractions,http:okHttp,authentication:azure,serialization:json,serialization:form,serialization:text,serialization:multipart,bundle).spotlessCheck), SpotBugs (spotbugsMain,spotbugsTest), Javadoc, JaCoCo, and all JUnit test suites passed.